package com.andy.blog.dal.service.impl;

import com.andy.blog.common.exception.BlogServiceException;
import com.andy.blog.common.reponse.BlogResultCode;
import com.andy.blog.common.utils.SecurityUtils;
import com.andy.blog.common.utils.TransformUtils;
import com.andy.blog.dal.domain.SensitiveDO;
import com.andy.blog.dal.mapper.TSensitiveMapper;
import com.andy.blog.dal.service.SensitiveDalService;
import com.andy.blog.dal.vo.SensitiveVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by andy on 2023/7/21 18:27
 */
@Service
public class SensitiveDalServiceImpl implements SensitiveDalService {

    @Resource
    private TSensitiveMapper sensitiveMapper;

    /**
     * 构建查询条件
     */
    private LambdaQueryWrapper<SensitiveDO> buildQueryWrapper(SensitiveVO sensitiveVO) {
        LambdaQueryWrapper<SensitiveDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .like(StringUtils.isNotBlank(sensitiveVO.getContent()), SensitiveDO::getContent, sensitiveVO.getContent())
                .orderByDesc(SensitiveDO::getCreateTime);
        return lambdaQueryWrapper;
    }

    @Override
    public List<SensitiveDO> selectList(SensitiveVO sensitiveVO) {
        LambdaQueryWrapper<SensitiveDO> lambdaQueryWrapper = buildQueryWrapper(sensitiveVO);
        return sensitiveMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public SensitiveDO selectOne(SensitiveVO sensitiveVO) {
        LambdaQueryWrapper<SensitiveDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SensitiveDO::getContent, sensitiveVO.getContent());
        return sensitiveMapper.selectOne(lambdaQueryWrapper);
    }

    @Override
    public IPage<SensitiveDO> selectPage(SensitiveVO sensitiveVO) {
        LambdaQueryWrapper<SensitiveDO> lambdaQueryWrapper = buildQueryWrapper(sensitiveVO);

        IPage<SensitiveDO> page = new Page<>(sensitiveVO.getPageNum(), sensitiveVO.getPageSize());

        return sensitiveMapper.selectPage(page, lambdaQueryWrapper);
    }

    @Override
    public void insertData(SensitiveVO sensitiveVO) {
        SensitiveDO sensitiveDO = TransformUtils.transform(sensitiveVO, SensitiveDO.class);

        sensitiveDO.setCreateBy(SecurityUtils.getUsername());

        int row;
        try {
            row = sensitiveMapper.insert(sensitiveDO);
        } catch (Exception e) {
            throw new BlogServiceException(BlogResultCode.BLOG_000100, e);
        }

        if (row <= 0) {
            throw new BlogServiceException(BlogResultCode.BLOG_000100);
        }
    }

    @Override
    public SensitiveDO selectById(Long id) {
        return sensitiveMapper.selectById(id);
    }

    @Override
    public void updateData(SensitiveVO sensitiveVO) {
        SensitiveDO sensitiveDO = TransformUtils.transform(sensitiveVO, SensitiveDO.class);

        sensitiveDO.setUpdateBy(SecurityUtils.getUsername());
        try {
            sensitiveMapper.updateById(sensitiveDO);
        } catch (Exception e) {
            throw new BlogServiceException(BlogResultCode.BLOG_000101);
        }
    }

    @Override
    public void deleteById(Long id) {
        try {
            sensitiveMapper.deleteById(id);
        } catch (Exception e) {
            throw new BlogServiceException(BlogResultCode.BLOG_000102, e);
        }
    }
}
